<template>
  <CommonSlot>
    <template v-slot:close-button>
      <span>点位信息</span>
      <el-button :icon="Close" circle @click="mapStore.pointManagementDiaolog.visible = false" />
    </template>
    <template v-slot:content>
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm"
        :size="formSize" status-icon>
        <el-form-item label="地址" prop="address">
          <el-input v-model="ruleForm.address" />
        </el-form-item>
        <el-form-item label="点位类型" prop="type">
          <el-select v-model="ruleForm.type" placeholder="点位类型">
            <el-option label="Zone one" value="shanghai" />
            <el-option label="Zone two" value="beijing" />
          </el-select>
        </el-form-item>
        <el-form-item label="网格区域" prop="grid">
          <el-select v-model="ruleForm.grid" placeholder="点位类型">
            <el-option label="Zone one" value="shanghai" />
            <el-option label="Zone two" value="beijing" />
          </el-select>
        </el-form-item>
        <el-form-item label="管理单位" prop="managementUnit">
          <el-select v-model="ruleForm.managementUnit" placeholder="管理单位">
            <el-option label="Zone one" value="shanghai" />
            <el-option label="Zone two" value="beijing" />
          </el-select>
        </el-form-item>
        <el-form-item label="范围查询" prop="queryScope">
          <el-segmented v-model="ruleForm.queryScope" :options="locationOptions" />
        </el-form-item>
      </el-form>
      <div class="el-button-group-custom">
        <el-button-group class="ml-4">
          <el-button type="primary" :icon="Search">查询</el-button>
          <el-button type="primary" :icon="CirclePlus">新增</el-button>
          <el-button type="primary" :icon="Upload">导入</el-button>
          <el-button type="primary" :icon="Download">导出</el-button>
        </el-button-group>
      </div>
      <el-table :data="tableData" style="width: 100%" :span-method="objectSpanMethod" max-height="200">
        <el-table-column prop="date" label="序号" width="60" />
        <el-table-column prop="address" label="地址" width="180" />
        <el-table-column prop="name" label="操作" />
      </el-table>
    </template>
  </CommonSlot>
</template>

<script setup>
import { ref, reactive } from "vue";
import {
  Close,
  Download,
  Search,
  CirclePlus,
  Upload,
} from "@element-plus/icons-vue";
import CommonSlot from "./CommonSlot.vue";
import pinia from "@/store/pinia.js";
import { useStore } from "@/store/index.js";
const mapStore = useStore(pinia);

const formSize = ref("default");
const ruleFormRef = ref();
const ruleForm = reactive({
  address: "",
  type: "",
  grid: "",
  managementUnit: "",
  queryScope: "",
});

const locationOptions = ["属性", "圆", "框"];

const rules = reactive({
  address: [
    { required: true, message: "请输入地址", trigger: "blur" },
    { min: 3, max: 5, message: "Length should be 3 to 5", trigger: "blur" },
  ],
  type: [
    {
      required: true,
      message: "Please select Activity zone",
      trigger: "change",
    },
  ],
  grid: [
    {
      required: true,
      message: "Please select Activity count",
      trigger: "change",
    },
  ],
  managementUnit: [
    {
      type: "date",
      required: true,
      message: "Please pick a date",
      trigger: "change",
    },
  ],
  queryScope: [
    {
      required: true,
      message: "Please select a location",
      trigger: "change",
    },
  ],
});

const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
    } else {
      console.log("error submit!", fields);
    }
  });
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));

const tableData = [
  {
    date: "1",
    name: "a1",
    address: "a2",
  },
  {
    date: "1",
    name: "a1-2",
    address: "a2-2",
  },
  {
    date: "1",
    name: "a1-3",
    address: "a2-3",
  },
  {
    date: "1",
    name: "a1-4",
    address: "a2-4",
  },
  {
    date: "2",
    name: "b1",
    address: "b2",
  },
  {
    date: "3",
    name: "c1",
    address: "c2",
  },
  {
    date: "4",
    name: "d1",
    address: "d2",
  },
];

/*const mergingRows = [];
let mergingPos;

const dataPretreatment = () => {
  //表格数据列合并预处理,生成一个与行数相同的数组记录每一行设置的合并数
  // 如果是第一条记录（索引为0），向数组中加入1，并设置索引位置；
  // 如果不是第一条记录，则判断它与前一条记录是否相等，如果相等，
  // 则向mergingRows中添入元素0，并将前一位元素+1，表示合并行数+1，
  // 以此往复，得到所有行的合并数，0即表示该行不显示。
  for (let i = 0; i < tableData.length; i++) {
    // tabledata 表格数据源
    if (i === 0) {
      mergingRows.push(1);
      mergingPos = 0;
    } else {
      if (tableData[i].date === tableData[i - 1].date) {
        //哪些数据是要合并的 合并的条件是什么
        mergingRows[mergingPos] += 1;
        mergingRows.push(0);
      } else {
        mergingRows.push(1);
        mergingPos = i;
      }
    }
  }
};

dataPretreatment();

const objectSpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex === 0) {
    //第一列
    const _row = mergingRows[rowIndex];
    const _col = _row > 0 ? 1 : 0;
    return {
      rowspan: _row,
      colspan: _col,
    };
  }
};
*/
</script>

<style lang='less' scoped>
@import url("./css/index.css");

.el-button-group-custom {
  width: 315px;
  margin: 0 auto !important;
}
</style>